package cn.kgc.springboot.mybatis.controller;

import cn.kgc.springboot.mybatis.entity.User;
import cn.kgc.springboot.mybatis.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/5/24
 * @Description:
 *用户模块：
 * 获取用户列表
 * http://localhost:8888/springboot/user/getById?id=1
 * http://localhost:8888/springboot/user/1
 */
@Controller
@RequestMapping("user")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping
    public String  getList(Model model){
        List<User> list = userService.getList();
        model.addAttribute("users",list);
        return "user";
    }

    //  @PathVariable("id")  restful 风格实现数据接收的方式
    @GetMapping("{id}")
    @ResponseBody
    public User  getList(@PathVariable("id") Integer id){
        return userService.getById(id);
    }



    @GetMapping("{page}/{limit}")
    @ResponseBody
    public List<User>  selectPage(@PathVariable("page") Integer page, @PathVariable("limit") Integer limit){
        List<User> list = userService.getPage(page,limit);
        return list;
    }

    @DeleteMapping("{id}")
    @ResponseBody
    public Map delete(@PathVariable("id") int id){
        return userService.deleteById(id);
    }


    @PutMapping
    @ResponseBody
    public Map update(@RequestBody User user){
        return userService.updateUser(user);
    }


    @PostMapping
    @ResponseBody
    public Map add(@RequestBody User user){
        return userService.addUser(user);
    }

}
